#include "main.h"

//LED灯
#define LED_GPIO_PIN        GPIO_PIN_13                 
#define LED_GPIO_PORT      	CW_GPIOC            
#define LEDON	  GPIO_WritePin(LED_GPIO_PORT,LED_GPIO_PIN,GPIO_Pin_RESET)
#define LEDOFF  GPIO_WritePin(LED_GPIO_PORT,LED_GPIO_PIN,GPIO_Pin_SET)

//方向控制端口定义
#define DR_GPIO_PIN        	GPIO_PIN_9                  
#define DR_GPIO_PORT      	CW_GPIOA
 
#define EN_GPIO_PIN        	GPIO_PIN_15                  
#define EN_GPIO_PORT      	CW_GPIOC   
 
#define FG_GPIO_PIN        	GPIO_PIN_12                  
#define FG_GPIO_PORT      	CW_GPIOA   
            
#define PWM_AP_PORT									(CW_GPIOB)
#define PWM_AP_PIN									(GPIO_PIN_5)
#define PWM_AN_PORT									(CW_GPIOA)
#define PWM_AN_PIN									(GPIO_PIN_15)

#define PWM_BP_PORT									(CW_GPIOB)
#define PWM_BP_PIN									(GPIO_PIN_6)
#define PWM_BN_PORT									(CW_GPIOB)
#define PWM_BN_PIN									(GPIO_PIN_3)

#define PWM_CP_PORT									(CW_GPIOB)
#define PWM_CP_PIN									(GPIO_PIN_7)
#define PWM_CN_PORT									(CW_GPIOB)
#define PWM_CN_PIN									(GPIO_PIN_4)


#define PWM_AH_OFF GPIO_WritePin(PWM_AP_PORT,PWM_AP_PIN,GPIO_Pin_RESET) 
#define PWM_BH_OFF GPIO_WritePin(PWM_BP_PORT,PWM_BP_PIN,GPIO_Pin_RESET)
#define PWM_CH_OFF GPIO_WritePin(PWM_CP_PORT,PWM_CP_PIN,GPIO_Pin_RESET)

#define PWM_AH_ON GPIO_WritePin(PWM_AP_PORT,PWM_AP_PIN,GPIO_Pin_SET) 
#define PWM_BH_ON GPIO_WritePin(PWM_BP_PORT,PWM_BP_PIN,GPIO_Pin_SET) 
#define PWM_CH_ON GPIO_WritePin(PWM_CP_PORT,PWM_CP_PIN,GPIO_Pin_SET) 

//上管调制，下管开关控制, 上高电平开关管导通
#define PWM_AL_OFF GPIO_WritePin(PWM_AN_PORT,PWM_AN_PIN,GPIO_Pin_RESET) 
#define PWM_BL_OFF GPIO_WritePin(PWM_BN_PORT,PWM_BN_PIN,GPIO_Pin_RESET) 
#define PWM_CL_OFF GPIO_WritePin(PWM_CN_PORT,PWM_CN_PIN,GPIO_Pin_RESET) 

#define PWM_AL_ON GPIO_WritePin(PWM_AN_PORT,PWM_AN_PIN,GPIO_Pin_SET)
#define PWM_BL_ON GPIO_WritePin(PWM_BN_PORT,PWM_BN_PIN,GPIO_Pin_SET)
#define PWM_CL_ON GPIO_WritePin(PWM_CN_PORT,PWM_CN_PIN,GPIO_Pin_SET)
              

//载波频率15k
#define PWM_PERIOD 6400 
// (96000000 / 15000 )-1; 

#define OnepercentPWM (PWM_PERIOD)/100

#define STATESTARTCHECK 0
#define STATESTARTOPEN 1
#define STATERUNOPEN 2
#define STATESTOP 3
#define STATEERROR 5
#define STATEERROROVER 6
#define STATWAITSTART 7
#define STATESTARTDELAY 8

//内部电位器
#define NMAXVD 3523
#define NMINVD 200
//0.244V~4.3V进行调节

#define NumErr 30
//过流过压错误认定次数

//过压值设定
#define RV1 5.1
#define RV2	200
//输入电压与总线电压之比：为RV1:(RV1+RV2)
     

extern unsigned int TimeCountCompuSpeed,TimeCountTemp,TimeCountSTDelay,Icount;
extern unsigned int TimeCountRealSpd,TimeCountPID,TimeCountAvgSpd, TimeCountVI,TimeCountStartUP;

extern unsigned char MOTORSTATE;

extern unsigned int TImeUP;//从最0占空比到100%的时间
//无感
extern unsigned char Tonoroff;

extern unsigned int StepTime;
extern unsigned char Sta;
extern unsigned int BEMFConvertedValue;

extern unsigned int bHallStartStep1;
extern unsigned char StOk;
extern unsigned char Tonoroff;
extern unsigned char QDPwm;

extern unsigned int STEP_last;


extern unsigned int PWMMin;//最低占空比
extern unsigned int PWMMax;//最大占空比输出
    
extern uint16_t SampleData[6];

extern unsigned char ErrorCode; //故障状态
extern unsigned int  Motor_Start_F; //电机启停状态
extern unsigned int  Cur_step; //当前换相相序
extern unsigned char Dir; //方向控制

extern unsigned int HALLcount,HALLcount1,HALLcountTemp; // 霍尔脉冲计数值，用于计算平均速度和瞬时速度
extern unsigned int TargS1;//PID目标速度
extern unsigned int RealS1;//实时瞬时速度
extern unsigned int RealS;//平均速度
extern unsigned int SetSpeed;
extern float KKN,KPWMN;//计算电位器速度时的斜率

extern unsigned int OutPwmValue;//输出占空比值
 
extern unsigned int SpeedD;//

extern unsigned int DIin; //电流偏置
extern unsigned int CanshuV;//母线电压100mV为单位
extern unsigned int CanshuI;//母线电流mA

extern unsigned int  MPolePairs;  //极对数
extern unsigned int  MAXSPEED; //额定转速
extern unsigned int  CanshuTimeDelay;//启动延时间MS

extern unsigned int  ISH;//过流报警值 A
extern unsigned int  VSH;//过压报警值 V
extern unsigned int  LSH;//欠压报警值 V

//速度闭环参数  实际缩小1000倍
extern unsigned int  MINSPEED;  //最低转速
extern unsigned int  CanshuSTT;//启动时间MS   时间参数 
//电流闭环参数  实际缩小1000倍  


extern unsigned int STEP_last;


extern long  aavsp;
extern unsigned int aacvsp;
extern long sumavsp;

extern unsigned int aa,bb,cc;
extern unsigned int aac,bbc,ccc;
extern unsigned int suma,sumb,sumc;

extern unsigned int bHallStartStep1;

extern unsigned int sumia,sumib,sumic;

extern unsigned int CanshuIA,CanshuIB,CanshuIC;//母线电流mA
